<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <input type="text" id="in-text">
  <button id="copy-btn">复制</button>
  <button id="po-btn">粘贴</button>
  <input type="text" id="input-text">
  <script>
    const ioBtn = document.querySelector('#in-text'); // 输入框
    const copyBtn = document.querySelector('#copy-btn'); // 复制按钮
    const poBtn = document.querySelector('#po-btn'); // 粘贴按钮
    const inBtn = document.querySelector('#input-text'); // 粘贴框
    copyBtn.addEventListener('click', copyToClipboard);
    poBtn.addEventListener('click', pasteFromClipboard);
    function copyToClipboard() {
      navigator.clipboard.writeText(ioBtn.value)
        .then(() => {
          console.log('复制成功');
        })
        .catch((error) => {
          console.error('复制失败', error);
        });
    }
    function pasteFromClipboard() {
      navigator.clipboard.readText()
        .then((text) => {
          inBtn.value = text;
          console.log('粘贴成功', text);
        })
        .catch((error) => {
          console.error('粘贴失败', error);
        });
    }

  </script>
</body>

</html>